package cn.tedu.blog.server.controller.system;

import cn.tedu.blog.server.pojo.dto.DownloadDTO;
import cn.tedu.blog.server.pojo.vo.ArticleDetailVO;
import cn.tedu.blog.server.service.IArticleService;
import cn.tedu.blog.server.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileOutputStream;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

@Slf4j
@RestController
public class DownloadController {

        @Autowired
        private IArticleService articleService;

    private static int count = 0;

    @PostMapping("/download")
    public JsonResult Download(@RequestBody DownloadDTO downloadDTO) throws IOException {
        log.error("收到传来的参数");
        File file = null;
        if (count > 0) {
            file = new File("E:/" + downloadDTO.getTitle() + count + ".md");
        } else {
            file = new File("E:/" + downloadDTO.getTitle() + ".md");
        }


        //判断文件夹是否存在
        if (!file.exists()) {
            file.createNewFile();
        } else {
            count++;
            downloadDTO.setTitle(downloadDTO.getTitle());
            Download(downloadDTO);
        }

        PrintWriter pw = new PrintWriter(file);
        pw.println("### " + downloadDTO.getTitle());
        pw.println(downloadDTO.getContent());
        System.out.println("写出完毕!");
        pw.close();

        return JsonResult.ok();
    }
}
